using UnityEngine;
using UnityEditor;
using System.Collections;


[CustomEditor(typeof(Volume))]
public class VolumeEditor : Editor {
	
    public override void OnInspectorGUI() 
	{
		Rect r;
		DrawDefaultInspector();
		
		r = EditorGUILayout.BeginHorizontal("Button");
		if(GUI.Button(r,GUIContent.none))
		{
			GenBlockedCamVolume();
		}
		GUILayout.Label ("Generate");
        GUILayout.Label ("Blocked Camera Volume");    
		EditorGUILayout.EndHorizontal();
		
		
		r = EditorGUILayout.BeginHorizontal("Button");
		if(GUI.Button(r,GUIContent.none))
		{
			GenTriggerVolume();
		}
		GUILayout.Label ("Generate");
        GUILayout.Label ("Trigger Volume");    
		EditorGUILayout.EndHorizontal();
	}
	private void GenTriggerVolume()
	{
		Volume thisVolume = ((Volume)target);
		thisVolume.volume = thisVolume.gameObject.GetComponent<BoxCollider>();
		
		thisVolume.type = VolumeType.VTTrigger;
	}
	private void GenBlockedCamVolume()
	{
		Volume thisVolume = ((Volume)target);
		thisVolume.volume = thisVolume.gameObject.GetComponent<BoxCollider>();
		
		GameObject father = thisVolume.gameObject;
		
		
		Renderer[] renderers = father.GetComponentsInChildren<Renderer>();
			
		Bounds largestBound = new Bounds(father.transform.position,Vector3.zero);
			
			
		foreach(Renderer renderer in renderers)
		{
						
				largestBound.Encapsulate(renderer.bounds);
				
		}
			
		thisVolume.volume.size = Quaternion.Inverse(father.transform.rotation) * largestBound.size;
		thisVolume.volume.center = largestBound.center - father.transform.position;
		
		thisVolume.type = VolumeType.VTCamera;
		thisVolume.volume.isTrigger = true;
	}
}
